package kitrobot.com.bed.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import kitrobot.com.bed.Activity.MainActivity;
import kitrobot.com.bed.Service.IdentifyingService;
import kitrobot.com.bed.Service.NewnameService;
import kitrobot.com.bed.Service.SeleuserService;
import kitrobot.com.wechat_bottom_navigation.R;

public class New_nameActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_nname;
    private String id;
    private String token;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.new_name);
        et_nname= (EditText) findViewById(R.id.et_nname);
        SharedPreferences sp = getSharedPreferences("loginToken",
                Context.MODE_PRIVATE);
        id = sp.getString("id", "");
        token = sp.getString("token", "");
        new Thread(){
            public void run(){
                final String result= SeleuserService.SeleuserByClientPost(id,token);
                if (result!=null){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                //JSON字符串
                                //根据JSON字符串 获得JSON object对象
                                JSONObject jsonObject = new JSONObject(result);
                                //根据key 获取相应的value
                                JSONObject data=jsonObject.getJSONObject("data");
                                name = data.getString("name");
                                et_nname.setText(name);
                                SharedPreferences sp = getSharedPreferences("loginToken", 0);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("name",name);
                                editor.commit();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    });
                }
                else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(New_nameActivity.this,"结果"+result,Toast.LENGTH_LONG).show();
                        }
                    });
                }
            };

        }.start();
    }
    public void Change(View view){
        final String nname=et_nname.getText().toString().trim();
        if (nname.length()==0){
            Toast.makeText(New_nameActivity.this,"用户名不能为空！",Toast.LENGTH_LONG).show();
        }
        else {
            new Thread(){
                public void run(){
                    final String result= NewnameService.NewnameByClientPost(id,nname,token);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String name=null;
                                String errno=null;
                                Toast.makeText(New_nameActivity.this,result,Toast.LENGTH_LONG).show();
                                try {
                                    //JSON字符串

                                    //根据JSON字符串 获得JSON object对象
                                    JSONObject jsonObject = new JSONObject(result);
                                    //根据key 获取相应的value
                                    errno=jsonObject.getString("errno");
                                    Toast.makeText(New_nameActivity.this,errno,Toast.LENGTH_LONG).show();
                                    if(errno.equals("0")){
                                        name = nname;
                                        System.out.println("名字="+nname);
                                        SharedPreferences sp = getSharedPreferences("loginToken", 0);
                                        SharedPreferences.Editor editor = sp.edit();
                                        editor.putString("name",name);
                                        editor.commit();
                                        finish();
                                    }

                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(New_nameActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();
        }
    }
    public void Caidan1(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",1);
        startActivity(intent);
    }
    public void Caidan2(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",2);
        startActivity(intent);
    }
    public void Caidan3(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",3);
        startActivity(intent);
    }
    public void Caidan5(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",4);
        startActivity(intent);
    }
    public void Finish(View view){
        finish();
    }
    @Override
    public void onClick(View view) {

    }

}
